/**
 * 
 */
package com.ideazd.api.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ideazd.api.model.TopicPost;
import com.ideazd.api.model.TopicPostComment;
import com.ideazd.api.model.dto.RetDTO;
import com.ideazd.api.tools.SysUtil;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * @author yzz
 * @create 2016年3月23日 下午4:47:01
 */
@ControllerBind(controllerKey="comment")
public class CommentController extends Controller {
	
	private static final Logger log = Logger.getLogger(CommentController.class);
	
	/**
	 * 评论列表
	 */
	public void getPostCommentList(){
		String posId = getPara("posId");
		String pageIndex = getPara("pageIndex");
		String pageSize = getPara("pageSize");
		Map<Object, Object> ret = new HashMap<Object,Object>();
		
		if(StrKit.isBlank(pageIndex)||StrKit.isBlank(pageSize)||StrKit.isBlank(posId)){
			ret.put("code","-2");
			ret.put("msg","必传参数不允许为空");
			renderJson(ret);
			return;
		}
		
		
		String sql = "from sta_topic_post_comment c left join sta_user u on u.useid = c.useid  where c.posid = '"+posId+"' order by createtime";
		
		try {
			
			Page<TopicPostComment> topicPostComment = TopicPostComment.dao.paginate(Integer.parseInt(pageIndex), Integer.parseInt(pageSize), "select c.*,u.nickname,u.headportrait ", sql);
			List<Map<Object, Object>> topicList = new ArrayList<Map<Object, Object>>();
			
			for(TopicPostComment comment : topicPostComment.getList()){
				Map<Object, Object> topic = new HashMap<Object,Object>();
				topic.put("comContent", comment.get("comcontent"));
				topic.put("nickName", comment.get("nickname"));
				topic.put("headPortrait", comment.get("headportrait"));
				topic.put("createTime", SysUtil.getPublishDate(comment.get("createtime").toString()));
				topicList.add(topic);
			}
			
			Map<Object, Object> data = new HashMap<Object,Object>();
			data.put("totalPage", topicPostComment.getTotalPage());
			data.put("topicPostComment", topicList);
			
			ret.put("code","0");
			ret.put("msg","成功");
			ret.put("data", data);
			
		} catch (Exception e) {
			ret.put("code","-1");
			ret.put("msg",e.getMessage());
		}
		
		renderJson(ret);
		return;
	}
	
	/**
	 * 帖子评论
	 */
	@Before(Tx.class)
	public void commentPost(){
		String topId = getPara("topId");
		String posId = getPara("posId");
		String useId = getPara("useId");
		String comContent = getPara("comContent");
		
        RetDTO ret = new RetDTO();
		
		if(StrKit.isBlank(topId)||StrKit.isBlank(useId)||StrKit.isBlank(posId)||StrKit.isBlank(comContent)){
			ret.setCode("-2");
			ret.setMsg("必传参数不允许为空");
			renderJson(ret);
			return;
		}
		
		try {
			
			String uuid = SysUtil.getUUID();
			
			TopicPostComment comment = new TopicPostComment();
			comment.set("comid", uuid);
			comment.set("topid", topId);
			comment.set("posid", posId);
			comment.set("useid", useId);
			comment.set("comcontent", comContent);
			comment.set("createtime", new Date());
			comment.set("delflag", 0);
			comment.save();
			
			Db.update("update sta_topic_postsssss set commentamount=commentamount+1 where posid = '"+posId+"'");
			
			TopicPost post = TopicPost.dao.findFirst("select * from sta_topic_post where posid = '"+posId+"' and useid = '"+useId+"'");
			
			if(null == post){
				Db.update("update sta_topic_post set unread = unread+1 where id = '"+posId+"'");
			}
			
			ret.setCode("0");
			ret.setMsg("成功");
			
		} catch (Exception e) {
			ret.setCode("-1");
			ret.setMsg(e.getMessage());
			try {
				DbKit.getConfig().getConnection().rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		renderJson(ret);
		return;
	}

}
